package com.pac.questionnaire.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.common.tools.StringTools;
import com.common.tools.TimeTools;
import com.common.util.ErrorNumber;
import com.common.util.LogicException;
import com.pac.questionnaire.QuestionnaireKeyContent;
import com.pac.questionnaire.bean.QuestionBean;
import com.pac.questionnaire.bean.QuestionOptionBean;
import com.pac.questionnaire.bean.QuestionnaireAnswerBean;
import com.pac.questionnaire.bean.QuestionnaireQuestionRelBean;
import com.pac.questionnaire.dao.QuestionDao;
import com.pac.questionnaire.service.QuestionOptionService;
import com.pac.questionnaire.service.QuestionService;
import com.pac.questionnaire.service.QuestionnaireAnswerService;
import com.pac.questionnaire.service.QuestionnaireQuestionRelService;

/**
 * 问题service实现
 * 
 * 
 * @author lujun.chen
 * @version [版本号, 2015年2月14日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service(value = "questionService")
public class QuestionServiceImpl implements QuestionService {

	private static Logger logger = Logger.getLogger(QuestionServiceImpl.class);

	@Autowired
	private QuestionDao questionDao;

	@Autowired
	private QuestionOptionService questionOptionService;

	@Autowired
	private QuestionnaireQuestionRelService questionnaireQuestionRelService;
	
	@Autowired
	private QuestionnaireAnswerService questionnaireAnswerService;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public String addQuestion(QuestionBean questionBean) {
		if (null == questionBean) {
			logger.error("添加问题出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(questionBean.getDescription())) {
			logger.error("添加问题出错，问题描述为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "问题描述为空");
		}

		if (StringTools.isNull(questionBean.getType())) {
			logger.error("添加问题出错，问题类型为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "问题类型为空");
		}

		if (StringTools.isNull(questionBean.getCreateTime())) {
			questionBean.setCreateTime(TimeTools.now());
		}

		if (StringTools.isNull(questionBean.getRights())) {
			questionBean
					.setRights(QuestionnaireKeyContent.QUESTION_RIGHTS_PRIVATE);
		}

		// 添加问题
		String questionId = questionDao.addQuestion(questionBean);

		// 添加问题选项
		List<QuestionOptionBean> optionList = questionBean.getQuestionOptions();
		if (null != optionList && optionList.size() > 0) {
			for (QuestionOptionBean option : optionList) {
				option.setQuestionId(questionId);
				questionOptionService.addQuestionOption(option);
			}
		}

		// 绑定问题和问卷关联关系
		if (!StringTools.isNull(questionBean.getQuestionnaireId())) {
			QuestionnaireQuestionRelBean questionnaireQuestionRelBean = new QuestionnaireQuestionRelBean();
			questionnaireQuestionRelBean.setQuestionId(questionId);
			questionnaireQuestionRelBean.setQuestionnaireId(questionBean
					.getQuestionnaireId());
			questionnaireQuestionRelService
					.addQuestionnaireQuestionRel(questionnaireQuestionRelBean);
		}

		return questionId;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean updateQuestion(QuestionBean questionBean) {
		if (null == questionBean) {
			logger.error("修改问题出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(questionBean.getId())) {
			logger.error("修改问题出错，问题ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "问题ID为空");
		}

		// 先删除选择题选项，再重新添加
		if (QuestionnaireKeyContent.QUESTION_TYPE_SINGLE_SELECT
				.equals(questionBean.getType())
				|| QuestionnaireKeyContent.QUESTION_TYPE_MULTI_SELECT
						.equals(questionBean.getType())) {
			
			QuestionOptionBean questionOptionBean=new QuestionOptionBean();
			questionOptionBean.setQuestionId(questionBean.getId());
			questionOptionService.delQuestionOption(questionOptionBean);
			
			List<QuestionOptionBean> options=questionBean.getQuestionOptions();
			for(QuestionOptionBean optionE:options)
			{
				optionE.setQuestionId(questionBean.getId());
				questionOptionService.addQuestionOption(optionE);
			}

		}

		return questionDao.updateQuestion(questionBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean delQuestion(QuestionBean questionBean) {
		if (null == questionBean) {
			logger.error("删除问题出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}
		if (StringTools.isNull(questionBean.getId())) {
			logger.error("删除问题出错，问题ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "问题ID为空");
		}

		// 同时删除问题下的选项
		QuestionOptionBean questionOptionBean = new QuestionOptionBean();
		questionOptionBean.setQuestionId(questionBean.getId());
		questionOptionService.delQuestionOption(questionOptionBean);

		boolean result = questionDao.delQuestion(questionBean);

		return result;
	}

	@Override
	public List<QuestionBean> queryQuestion(QuestionBean questionBean) {
		return questionDao.queryQuestion(questionBean);
	}

	@Override
	public List<QuestionBean> queryQuestionByQuestionnaireId(
			String questionnaireId) {

		if (StringTools.isNull(questionnaireId)) {
			return null;
		}

		QuestionBean questionBean = new QuestionBean();
		questionBean.setQuestionnaireId(questionnaireId);
		List<QuestionBean> questionList = questionDao
				.queryQuestion(questionBean);

		// 单选题，多选题获取选项
		for (QuestionBean question : questionList) {
			if (QuestionnaireKeyContent.QUESTION_TYPE_SINGLE_SELECT
					.equals(question.getType())
					|| QuestionnaireKeyContent.QUESTION_TYPE_MULTI_SELECT
							.equals(question.getType())) {
				QuestionOptionBean questionOptionBean = new QuestionOptionBean();
				questionOptionBean.setQuestionId(question.getId());
				List<QuestionOptionBean> optionList = questionOptionService
						.queryQuestionOption(questionOptionBean);
				question.setQuestionOptions(optionList);
			}
		}

		return questionList;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean editQuestion(Map<String, String[]> answerMap) {
		
		Set<String> keysets = answerMap.keySet();
		for (String key : keysets) {
			if (key.startsWith(QuestionnaireKeyContent.QUESTION_ANSWER_KEY)) {
				// 问题ID
				String[] answers = answerMap.get(key);
				String[] answerArr=key.split("_");
				String questionType=answerArr[1];
				String answerid=answerArr[2];
				//问答题
				if(QuestionnaireKeyContent.QUESTION_TYPE_INTERLOCUTION.equals(questionType))
				{
					QuestionnaireAnswerBean questionnaireAnswerBean =new QuestionnaireAnswerBean();
					questionnaireAnswerBean.setId(answerid);
					questionnaireAnswerBean.setAnswer(answers[0]);
					questionnaireAnswerService.updateQuestionnaireAnswer(questionnaireAnswerBean);
				}
			}
		}
		
		return true;
	}
	
}
